package com.lb.shopguide.ui.fragment.guide.pay;

import android.os.Bundle;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.lb.baselib.http.ResponseCode;
import com.lb.baselib.http.SimpleLbObserver;
import com.lb.baselib.http.bean.BaseResponse;
import com.lb.shopguide.R;
import com.lb.shopguide.base.BaseCommonFragment;
import com.lb.shopguide.constants.AppConstant;
import com.lb.shopguide.entity.AlipayAccountBean;
import com.lb.shopguide.event.guide.AlipayAccountChangeEvent;
import com.lb.shopguide.http.method.ApiMethodGuide;
import com.lb.baselib.http.SimpleObserverOnNextListener;
import com.lb.shopguide.ui.activity.GuideMainActivity;
import com.lb.shopguide.ui.view.NormalTitleBar;
import com.lb.shopguide.util.json.JsonUtil;
import com.trello.rxlifecycle2.android.FragmentEvent;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by kang on 2017/9/6.
 */

public class FragmentAlipayAccountDetail extends BaseCommonFragment {
    @BindView(R.id.ntb)
    NormalTitleBar ntb;
    @BindView(R.id.tv_alipay_account)
    TextView tvAlipayAccount;
    @BindView(R.id.tv_real_name)
    TextView tvRealname;

    private AlipayAccountBean accountBean;

    public static FragmentAlipayAccountDetail newInstance(AlipayAccountBean bean) {
        FragmentAlipayAccountDetail fragment = new FragmentAlipayAccountDetail();
        Bundle bundle = new Bundle();
        bundle.putSerializable(AppConstant.ALIPAY_ACCOUNT_BEAN, bean);
        fragment.setArguments(bundle);
        return fragment;
    }



    @Override
    public int getLayoutId() {
        return R.layout.fragment_alipay_account_detail;
    }

    @Override
    public void initData() {
        Bundle bundle = getArguments();
        if (bundle.containsKey(AppConstant.ALIPAY_ACCOUNT_BEAN)) {
            accountBean = (AlipayAccountBean) bundle.getSerializable(AppConstant.ALIPAY_ACCOUNT_BEAN);
        }
        EventBus.getDefault().register(this);
    }

    @Override
    public void initView() {

        ntb.setTitleText("收款支付宝");
        ntb.setLeftImageSrc(R.drawable.iv_back);
        ntb.setRightText("重置");

        bindData();
    }

    @Override
    public void finishRefresh() {

    }

    @Override
    public void sessionOutOfDate() {
        ((GuideMainActivity) getActivity()).onSessionOutOfDate();
    }

    @Override
    public void requestError(String error) {
        ToastUtils.showShort(error);
    }

    @Override
    public String getPageTag() {
        return FragmentAlipayAccountDetail.class.getName();
    }

    private void bindData() {
        tvRealname.setText(accountBean.getReceiveTrueName());
        tvAlipayAccount.setText(accountBean.getReceiveAccount());
    }


    private void sendRequestGetAlipayAccount() {
        SimpleObserverOnNextListener<BaseResponse> listener = new SimpleObserverOnNextListener<BaseResponse>() {

            @Override
            public void onNext(BaseResponse response) {
                if (response.getReturnCode() == ResponseCode.RESPONSE_SUCCESS) {
                    accountBean = (AlipayAccountBean) JsonUtil.getObject(response.getReturnContent(), AlipayAccountBean.class);
                    bindData();
                }
            }
        };
        ApiMethodGuide.getAlipayAccount(new SimpleLbObserver(listener,otherListener),this, FragmentEvent.DESTROY_VIEW);
    }

    @OnClick(R.id.image_left)
    void leftClick() {
        pop();
    }

    @OnClick(R.id.tv_right)
    void rightClick() {
        start(FragmentSetAlipayAccount.newInstance(AppConstant.RESET_ALIPAY_ACCOUNT, accountBean));
    }

    @Subscribe
    public void accountChange(AlipayAccountChangeEvent event) {
        sendRequestGetAlipayAccount();
    }

    @Override
    public void onDestroyView() {
        EventBus.getDefault().unregister(this);
        super.onDestroyView();
    }
}
